package com.neuedu.hcp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.hcp.config.security.JwtTokenUtil;
import com.neuedu.hcp.entity.RecoveryPlan;
import com.neuedu.hcp.entity.RespBean;
import com.neuedu.hcp.entity.RespPageBean;
import com.neuedu.hcp.entity.Staff;
import com.neuedu.hcp.mapper.StaffMapper;
import com.neuedu.hcp.service.IStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 第八组
 * @since 2021-04-26
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {


    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    StaffMapper staffMapper;
    @Resource
    IStaffService staffService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;
    /**
     * 根据用户名获取用户
     * @param username
     * @return
     */
    @Override
    public Staff getStaffByUsername(String username) {
        return getBaseMapper().selectOne(new QueryWrapper<Staff>()
                                     .eq("username",username));
    }

    /**
     * 登陆之后生成token
     * @param username
     * @param password
     * @param code
     * @param request
     * @return
     */
    @Override
    public RespBean login(String username, String password, String code, HttpServletRequest request) {
        String captcha = (String) request.getSession().getAttribute("captcha");
        if(null==code || "".equals(code) || !captcha.equalsIgnoreCase(code)){
            return RespBean.error("验证码错误，请重新输入");
        }

        //登录
        UserDetails userDetails =userDetailsService.loadUserByUsername(username);
        if(null==userDetails||!passwordEncoder.matches(password,userDetails.getPassword())){
            return RespBean.error("用户名或密码不正确");
        }
        if(!userDetails.isEnabled()){
            return RespBean.error("账号被禁用，请联系管理员");
        }
        //更新security登录对象
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);

        //生成token
        String token =jwtTokenUtil.generateToken(userDetails);
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        return RespBean.success("登录成功",tokenMap);
    }
//用户管理
    @Override
    public RespPageBean getStaffListByPage(Integer currentPage, Integer size, Staff staff) {
        Page<Staff> page = new Page<>(currentPage,size);
        IPage<Staff> staffByPage =staffMapper.getStaffListByPage(page,staff);
        RespPageBean respPageBean = new RespPageBean(staffByPage.getTotal(),staffByPage.getRecords());
        return respPageBean;
    }

    @Override
    public int insert(Staff staff) {

        return staffMapper.insert(staff);
    }

}
